package com.ruoyi.model.config;

import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
import org.springframework.beans.factory.annotation.Qualifier;

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;

import java.sql.Connection;
import java.sql.SQLException;


/**
 * @program: ruoyi
 * @description: hive数据库配置类
 * @author: tangyongbo
 * @create: 2021-05-25 14:43
 **/
@Configuration
public class HiveJdbcConfig {
//    @Value("${hive.url}")
//    private String url;
//
//    @Value("${hive.driver-class-name}")
//    private String driver;
//
//    @Value("${hive.user}")
//    private String user;
//
//    @Value("${hive.password}")
//    private String password;
//
//    @Bean
//    public DataSource dataSource(){
//        DataSource dataSource = new DataSource();
//        dataSource.setUrl(url);
//        dataSource.setDriverClassName(driver);
//        dataSource.setUsername(user);
//        dataSource.setPassword(password);
//        return dataSource;
//    }
//
//    @Bean
//    public JdbcTemplate jdbcTemplate(DataSource dataSource){
//        return new JdbcTemplate(dataSource);
//    }

    @Bean(name = "hiveDataSource")
    @ConfigurationProperties(prefix = "hive.datasource.druid")//和yml中的配置项，呼应
    public DruidDataSource dataSource() {
        return DruidDataSourceBuilder.create().build();
    }

    @Bean(name = "hiveJdbcTemplate")
    public JdbcTemplate getJdbcTemplate(@Qualifier("hiveDataSource") DruidDataSource dataSource){
        return new JdbcTemplate(dataSource);
    }

    /**
     * 获取hive连接
     */
    public Connection getHiveConnection() throws SQLException {
        return this.dataSource().getConnection();
    }
}
